package com.jsz.peini.server.thread;

import java.util.List;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jsz.peini.bean.order.OrderInfo;
import com.jsz.peini.bean.task.TaskInfo;
import com.jsz.peini.common.util.SpringContextUtil;
import com.jsz.peini.mapper.order.OrderInfoMapper;
import com.jsz.peini.mapper.task.TaskInfoMapper;

public class TaskCancelTimerTask extends TimerTask {
	private static final Logger logger = LoggerFactory.getLogger(TaskCancelTimerTask.class);
	
	OrderInfoMapper orderInfoMapper = (OrderInfoMapper)SpringContextUtil.getBean(OrderInfoMapper.class);
	
	TaskInfoMapper taskInfoMapper = (TaskInfoMapper)SpringContextUtil.getBean(TaskInfoMapper.class);
	//是否有人付款
	private boolean isPay;
	//任务ID
	private Integer taskId;
	
	@Override
	public void run() {
						isPay = false;
						OrderInfo orderInfo = new OrderInfo();
						orderInfo.setTaskInfoId(getTaskId());
						List<OrderInfo> list = orderInfoMapper.selectOrderInfoByTaskId(orderInfo);
						if (list.size() == 0) {
							TaskInfo taskInfo = new TaskInfo();
							taskInfo.setId(getTaskId());
							taskInfo.setTaskStatus(5);
							taskInfoMapper.updateByPrimaryKeySelective(taskInfo);
							logger.info("双方均未支付,因此任务被系统取消");
						}else {
							for (OrderInfo order : list) {
								if (order.getPayStatus() == 1) {
									isPay = true;
								}
								
							}
							if (isPay == false) {
								orderInfoMapper.updateOrderInfoCancelByTaskId(orderInfo);
								TaskInfo taskInfo = new TaskInfo();
								taskInfo.setId(getTaskId());
								taskInfo.setTaskStatus(5);
								taskInfoMapper.updateByPrimaryKeySelective(taskInfo);
								logger.info("双方支付流程均未完成,因此任务被系统取消");
							}
						}

	}



	public Integer getTaskId() {
		return taskId;
	}



	public void setTaskId(Integer taskId) {
		this.taskId = taskId;
	}

}
